package fieldgame;

import boundaryToMatador.GUI;
import core.*;

public class GameManager extends GameManagerAbs {

	private UIManager uiManager;
	PlayerAbs[] victoryPlayers;

	public void initialize() {
		victoryPlayers = new Player[0];
		uiManager = new UIManager();
		int numberOfPlayers = uiManager.numberOfPlayersEvent(2, 6);
		// Inserts the number of players decided by the actor
		game = new Game(numberOfPlayers);
		// Creates a shaker with 2 dices with 6 sides each
		shaker = new Shaker(2, 6);
		uiManager.initPlayers(((Game) game).getStartBalance(), numberOfPlayers);
	}

	public void match() {
		GUI.getUserButtonPressed("", "start match");
		// resets the game and GUI before starting a new match
		((Game) game).reset();
		victoryPlayers = new Player[0];
		uiManager.reset(((Game) game).getStartBalance(),
				game.getNumberOfPlayers());
	}

	public void endMatch() {

		int[] winners;
		int size = 0;
		int winnerBalance = 0;
		int index = 0;
		// finds the player with the highest balance, and counts how many
		// players that got the same balance
		for (int i = 0; i < victoryPlayers.length; i++) {
			int playerBalance = ((Player) victoryPlayers[i]).getBalance();
			if (playerBalance == winnerBalance) {
				size++;
			} else if (playerBalance > winnerBalance) {
				winnerBalance = playerBalance;
				size = 1;
			}
		}

		// converts the players with the shared highest victory balance, and put
		// them in a new list, the victory player list
		winners = new int[size];
		for (int i = 0; i < victoryPlayers.length; i++) {
			if (((Player) victoryPlayers[i]).getBalance() == winnerBalance) {
				System.out.println(victoryPlayers[i].getNumber() + " " +((Player) victoryPlayers[i]).getBalance()  );
				winners[index] = victoryPlayers[i].getNumber();
				index++;
			}
		}
		System.out.println(winnerBalance);
		uiManager.displayWinner(winners, winnerBalance);
	}

	public boolean isMatchDone() {
		boolean isDone = victoryPlayers.length > 0;
		return isDone;
	}

	public void round() {
	}

	public void endRound() {
		victoryPlayers = game.getVictoryPlayers();
	}

	public boolean isRoundDone() {
		Player p = (Player) game.getCurrentPlayer();
		boolean isDone = game.isLastPlayer();

		return isDone;
	}

	public void turn() {
		Player p = (Player) game.getCurrentPlayer();
		Game g = (Game) game;

		// roll the dices
		shaker.shake();
		// update the GUI to match the rolled dices
		uiManager.shake(shaker.getDiceSum(), shaker.getDices()[0].getValue(),
				shaker.getDices()[1].getValue(), p.getNumber());

		// make the current player enter the field
		g.enterField(shaker.getDiceSum());
		uiManager
				.updatePlayer(p.getNumber(), p.getBalance(), p.getFieldIndex());

		// if the balance is meeting the victory requirement, enable the payers
		// victory state
		if (p.getBalance() >= g.getVictoryBalance())
			p.setVictoryState(true);

		// if the player doesn't land on a field that gives an extra turn, it
		// becomes the next players turn
		if (!((Game) game).getFields()[p.getFieldIndex()].getExtraTurn())
			game.nextPlayer();
	}



	public static void main(String[] args) {
		new GameFlow(new GameManager());
	}

}
